﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace HY.WEB.SA
{
    public partial class comp_edit : PageBase
    {

        string sid = "";
        public string us,um,tab="company";
        public BLL.CO.company cpy = new BLL.CO.company();

        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["sid"]))
                {
                    sid = Request["sid"].Trim();

                    hidsid.Text = sid;
                    fill();
                    BindText(sid);
                }
            }

        }

        void fill()
        {

            //行业分类
            DataTable e = BLL.Bllbase.get_table("[guest_hy]");
            ddlhy.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlhy.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddlhy.DataBind();

            //客户分类
            e = BLL.Bllbase.get_table("[guest_type]");
            ddltype.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddltype.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddltype.DataBind();

            //客户性质
            e = BLL.Bllbase.get_table("[guest_xz]");
            ddlxz.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlxz.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddlxz.DataBind();


            //区域
            e = BLL.Bllbase.get_table("[pub_area]");
            ddlcity.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlcity.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["svalue"].ToString(), e.Rows[i]["scode"].ToString()));
            }
            ddlcity.DataBind();
            //业务级别
            e = BLL.Bllbase.get_table("[sys_level]");
            ddllevel.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddllevel.Items.Add(new ExtAspNet.ListItem(e.Rows[i]["name"].ToString(), e.Rows[i]["name"].ToString()));
            }
            ddllevel.DataBind();



            DataTable d = cpy.GetList("sid=" + sid + "").Tables[0];

            //行业分类
            ddlhy.SelectedIndex = ddlhy.Items.IndexOf(ddlhy.Items.FindByText(d.Rows[0]["hytype"].ToString()));
            //客户分类
            ddltype.SelectedIndex = ddltype.Items.IndexOf(ddltype.Items.FindByText(d.Rows[0]["salestype"].ToString()));
            //客户性质
            ddlxz.SelectedIndex = ddlxz.Items.IndexOf(ddlxz.Items.FindByText(d.Rows[0]["cntype"].ToString()));

            //地区
            ddlcity.SelectedIndex = ddlcity.Items.IndexOf(ddlcity.Items.FindByValue(d.Rows[0]["city"].ToString()));
            //业务级别
            ddllevel.SelectedIndex = ddllevel.Items.IndexOf(ddllevel.Items.FindByValue(d.Rows[0]["slevel"].ToString()));

        }

        void BindText(string sid)
        {
            DataTable dt = cpy.GetList("sid=" + sid).Tables[0];

            if (dt.Rows.Count > 0)
            {
                hidsid.Text = dt.Rows[0]["sid"].ToString();
                txtnm.Text = dt.Rows[0]["cname"].ToString();
                hidbh.Text = dt.Rows[0]["cbh"].ToString();

                string   rdate = dt.Rows[0]["reg_date"].ToString().Trim();
                if (rdate.Substring(0, 4) != "1900")
                {
                  DateTime   lastday = DateTime.Parse(rdate);
                  date1.Text = lastday.ToString("yyyy-M-d");
                }
              
                txtjob.Text = dt.Rows[0]["job"].ToString();
                txtsex.Text = dt.Rows[0]["sex"].ToString();
                txtqq.Text = dt.Rows[0]["qq"].ToString();
                txtmsn.Text = dt.Rows[0]["msn"].ToString();
                txtcompnum.Text = dt.Rows[0]["compnum"].ToString();

                txtaddress.Text = dt.Rows[0]["addresss"].ToString();
                txtpost.Text = dt.Rows[0]["post"].ToString();
                txtperson.Text = dt.Rows[0]["person"].ToString();

                txttel.Text = dt.Rows[0]["ctel"].ToString();
                txtfax.Text = dt.Rows[0]["cfax"].ToString();
                txtwebsite.Text = dt.Rows[0]["website"].ToString();
                txtmail.Text = dt.Rows[0]["email"].ToString();
                txtmtel.Text = dt.Rows[0]["mtel"].ToString();

               if(dt.Rows[0]["yn"].ToString()=="1") chbyn.Checked=true;

                FCKeditor1.Value = HttpUtility.HtmlDecode(dt.Rows[0]["memo"].ToString());
            
            }

        }


      

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Model.CO.company CP = new Model.CO.company();
            CP.sid =int.Parse( hidsid.Text);
            CP.cbh = hidbh.Text;
            if (CP.cbh == "") return;
            CP.cname = txtnm.Text.Trim();

            if (!VAN.IsDateTime(date1.Text))
                CP.reg_date = DateTime.Parse("1900-01-01");
            else
                CP.reg_date = DateTime.Parse(date1.Text.Trim());
            
            CP.cntype = ddlxz.SelectedValue;
            CP.addresss = txtaddress.Text;
            CP.compnum = txtcompnum.Text;
            CP.city = ddlcity.SelectedValue;

            CP.person = txtperson.Text;
            CP.ctel = txttel.Text;
            CP.qq = txtqq.Text;
            CP.msn = txtmsn.Text;
            CP.sex = txtsex.Text;
            CP.job = txtjob.Text;
            CP.level = ddllevel.SelectedValue;

            CP.cfax = txtfax.Text;
            CP.post = txtpost.Text;
            CP.website = txtwebsite.Text;
            CP.email = txtmail.Text;
            CP.mtel = txtmtel.Text;
            CP.cnum = 0;
            CP.ht = 0;

            CP.salestype = ddltype.SelectedValue;

            CP.cstatus = 0;

            CP.hytype = ddlhy.SelectedValue;

            CP.memo = HttpUtility.HtmlEncode(FCKeditor1.Value);

            BLL.CO.company CY = new BLL.CO.company();
            CY.Update(CP);

            string yn = "0";
            if (chbyn.Checked) yn = " 1";
            BLL.Bllbase.up_table(tab, "yn=" + yn + "", " cbh='" + CP.cbh + "' ");

            //更新业务员操作记录
            BLL.Bllbase.update_rec(tab, um, " cbh='" + CP.cbh + "' ");

            Alert.Show("修改完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
       
        }

       
    }
}